%%
% Author : xiaoyeyimier
% Date : 21/10/3
% Version : 1.1
%------------------------------------------------------------------------%
% Author : xiaoyeyimier
% Date : 19/9/19
% Version : 1.0
% test successful,but Unable to parse GPS data
%------------------------------------------------------------------------%
%settings
% - sampletime : Time to sample
% - num_rxs    : Number of transmission
% - Fc         : Center Frequency
% - Fs         : Sampling Frequency
% - Bw_rx      : BandWidth
% - num_buf    : Szie of Buffer

%优化
% 2022-06-10
% 1.一维数组存储
% 2.数据类型压缩
% 3.内存映射
% 4.并行的尝试(不行)
% 5.取消进度条
function[SaveFileFullPathI,SaveFileFullPathQ]=GPS_bladeRF(Dev,settings)

TempNameI = sprintf('%s_IData.%s',...
    settings.FileName,...
    settings.FileType);
TempNameQ = sprintf('%s_QData.%s',...
    settings.FileName,...
    settings.FileType);

SaveFileFullPathI = [settings.SavePath,'\',TempNameI];
SaveFileFullPathQ = [settings.SavePath,'\',TempNameQ];
FidI = fopen(SaveFileFullPathI,'wb');
FidQ = fopen(SaveFileFullPathQ,'wb');

total_sec=settings.sampletime;                                         % Total sampling time [s]
n_sec = 200;                                                           % Single sampling time [ms]
LoopTimes=ceil(total_sec/n_sec*1e3);                                   % loop time
Fs=settings.Fs;
Fc=settings.Fc;
num_rxs=settings.num_rxs;
SamplesPerRx = floor(Fs * n_sec/1e3/num_rxs);                          % The number of sample...
% points in a single transmission
%---------------------------- SET BLADERF --------------------------------%
Dev.rx.config.num_buffers = settings.num_buf;
Dev.rx.frequency  = Fc;
Dev.rx.samplerate = Fs;
Dev.rx.bandwidth  = settings.Bw_rx;
Dev.rx.channel = settings.channel;
Dev.rx.biastee = 1;
%     Dev.rx.agc     = settings.agc;
disp('BladeRF will work in the following configuration:');
Dev.rx
Dev.rx.config
Dev.rx.start();
% h=waitbar(0,'Sampleing'); % Create a waitbar
tic;
%---------------------------- READ DATA ----------------------------------%
for loop_cnt=1:LoopTimes
    rxs=zeros(1,num_rxs*SamplesPerRx);                % initial buffer
%     waitbar(loop_cnt/LoopTimes,h);                   % Underrun show schedule
    disp([num2str(loop_cnt/LoopTimes*100),'%']);
    idx = 1:SamplesPerRx;
    for readloop = 1:num_rxs                           % read data in a loop
        [rxs(idx), ~, ~, ~] = Dev.receive(SamplesPerRx); % read data single
        idx = idx+SamplesPerRx;
    end
    if strcmpi(settings.dataType,'int8')
        samples_I = int8(real(rxs)*128);
        samples_Q = int8(imag(rxs)*128);
    else
        samples_I = real(rxs);
        samples_Q = imag(rxs);
    end
    fwrite(FidI,samples_I,settings.dataType);
    fwrite(FidQ,samples_Q,settings.dataType);
end
pp=toc;
fprintf('toatal time is %.2f',pp);
%--------------------------- CLOSE DEVICE -------------------------------%
% close(h);      % delete waitbar
Dev.rx.stop(); % close rx channel
fclose(FidI);  % close Idata file handle
fclose(FidQ);  % close Qdata file handle
end